package com.mongdb.mongdb;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

@Component
public class AsyncTask {
	@Autowired
	private MongoTemplate mongoTemplate;

	@Async
	public Future<String> asyncInsert(User user){
		System.out.println("执行async插入"+user.getId()+Thread.currentThread().getName());

		mongoTemplate.save(user);
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return new AsyncResult<>("user"+user.getId());
	}

}
